<?php
// +----------------------------------------------------------------------+
// | Author:  Evgeny Leontev <eleontev@gmail.com>                         |
// | Copyright (c) 2005 Evgeny Leontev                                    |
// +----------------------------------------------------------------------+
// | This source file is free software; you can redistribute it and/or    |
// | modify it under the terms of the GNU Lesser General Public           |
// | License as published by the Free Software Foundation; either         |
// | version 2.1 of the License, or (at your option) any later version.   |
// |                                                                      |
// | This source file is distributed in the hope that it will be useful,  |
// | but WITHOUT ANY WARRANTY; without even the implied warranty of       |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU    |
// | Lesser General Public License for more details.                      |
// +----------------------------------------------------------------------+

/**
 * SortOrder is a class to generate sort links.
 *
 * @version 1.0
 * @since 06/15/2004
 * @author Evgeny Leontev 
 * @access public
 *
 * EXAMPLE
 * 
 *
 * CHANGELOG
 * 06/15/2004 - release
 */
class SortOrder
{
	
	var $use_session = false;
	var $sql;
	var $query_string;
	
	var $sort_param = 'sort';
	var $order_param = 'order';
	var $sort_order = array(1=>'asc', 2=>'desc');
	var $default_order = 2;
	
	var $title_msg = array(1=>'Sort ascending', 2=>'Sort descending');
	
	var $a_class = 'sort';
	var $img_path = 'images/icons/';
	var $spacer_img_path = 'images/s.gif';
	var $img_asc = 'sort_asc.gif';
	var $img_desc = 'sort_desc.gif';
	var $img_asc_active = 'sort_asc_active.gif';
	var $img_desc_active = 'sort_desc_active.gif';	
	
	var $sort_ar = array();
	var $sql_command = 'ORDER BY';
	
	
   /**
	* constructor 
	*/
	function SortOrder($vars) {
		$this->setQueryString($vars);
	}
	
	
   /**
	* setSortItem -- set sort item
	*
	* @param    string   $tpl_var      name of tpl var in template	
	* @param    string   $get_param    what apears in $_GET string
	* @param    string   $sql_param    sql params use in sql (for instance i.date)
	* @param    string   $field_name   field name(what in template as collumn name)
	* @param    string   $order        (optional) if specify it will be a default sort param
	*
	* @return   void
	* @access   public    
	*/
	function setSortItem($tpl_var, $get_param, $sql_param, $field_name = false, $order = false) {

		$this->sort_ar[$tpl_var] = array('get_param'=>$get_param, 
										 'sql_param'=>$sql_param,
										 'order'=>$order,
										 'field_name'=>$field_name);
	}
	
	
  /**
	* setQueryString -- set string that appends in sort links
	*
	* @param    array    $arr       usually $_GET
	*
	* @return   void
	* @access   public    
	*/
	function setQueryString($arr) {
		
		unset($arr[$this->sort_param], $arr[$this->order_param]);// skip $_GET['sort']
		$this->query_string = $_SERVER['PHP_SELF'].'?';
		$this->query_string .= http_build_query($arr);
	}
	
	
   /**
	* getOrder -- get order string
	*
	* @param    mixed    $val     asc or desc or 1 or 2
	*
	* @return   string   asc or desc
	* @access   private    
	*/
	function _getOrder($val) {
		$order = (is_numeric($val)) ? $this->sort_order[$val] : $val;
		return $order;
	}
	
	
   /**
	* getLink -- get link for order item
	*
	* @param    sort     $sort     param for sorting
	* @param    int      $order    param for order (1 or 2)	
	*
	* @return   string   asc or desc
	* @access   private    
	*/
	function getLink($sort, $order) {
		$link = $this->query_string . '&' . $this->sort_param.'='.$sort.'&'.$this->order_param.'='. $order;
		return $link;
	}
	
	
	// will be in foreach return number or false
	function getOrder($order, $get_param) {
			
			// default
			if($order !== false && !isset($_GET[$this->sort_param])) {
				//$order = $this->default_order;
				$order = $this->_getOrder($order);
			
			// have a $_GET['sort']
			} elseif ($get_param == @$_GET[$this->sort_param]) {
				if(isset($this->sort_order[$_GET[$this->order_param]])) {
					$order = $_GET[$this->order_param];
				} else {
					$order = $this->default_order;
				}
				
			} else {
				$order = false;
			} 
		
		return $order;
	}
	
	
	function setSql($sort, $order) {
		//$this->sql = "ORDER BY {$sort} {$order}";
		$this->sql[] = "{$sort} {$order}";
	}
	
	
	function getSql() {
		
		foreach($this->sort_ar AS $tpl_var => $v) {
			
			$order = $this->getOrder($v['order'], $v['get_param']);
			if($order !== false) { 
				$sql = explode(',', $v['sql_param']);
				foreach($sql as $param) {
					$this->setSql($param, $this->_getOrder($order));
				} 
			}
		}	
		
		if($this->sql) {
			return 'ORDER BY ' . implode(', ', $this->sql);
		}
	}
	
	
	function setTitleMsg($type, $msg) {
		$order = array_search($type, $this->sort_order);
		$this->title_msg[$order] = $msg;
	}
}


/*
class TwoWaySort extends SortOrder
{
	
	function & toHtml($sort, $active = false) {
		
		if($active) {
			$img = ($active == 'asc') ? $this->img_asc_active : $this->img_desc_active;
			$order = ($active == 'asc') ? 1 : 2;
			$html[$order] = '<img src="'.$this->img_path.$img.'" border=0>';
			
			$img = ($active == 'asc') ? $this->img_desc : $this->img_asc;
			$order = ($active == 'asc') ? 2 : 1;
			$html[$order] = '<a href="'.$this->getLink($sort, $order).'"><img src="'.$this->img_path.$img.'" border=0></a>';
		} else {
			$html[1] = '<a href="'.$this->getLink($sort, 1).'"><img src="'.$this->img_path.$this->img_asc.'" border=0></a>';
			$html[2] = '<a href="'.$this->getLink($sort, 2).'"><img src="'.$this->img_path.$this->img_desc.'" border=0></a>';
		}
	
		
		ksort($html);
		$html = '<div align="right">' . implode('', $html) . '</div>';
		return $html;
	}
	
	
	function & getVars() {
	
		foreach($this->sort_ar AS $tpl_var => $v) {
			
			if($v['order'] !== false && !isset($_GET[$this->sort_param])) {
				$order = $this->getOrder($v['order']);
				$this->setSql($v['sql_param'], $order);
			
			} elseif ($v['get_param'] == @$_GET[$this->sort_param]) {
				if(isset($this->sort_order[$_GET[$this->order_param]])) {
					$order = $this->sort_order[$_GET[$this->order_param]];
				} else {
					$order = $this->sort_order[2]; // default order desc
				}
				$this->setSql($v['sql_param'], $order);
				
			} else {
				$order = false;
			} 
			
			
			$ar[$tpl_var] = $this->toHtml($v['get_param'], $order);
		}
		
		return $ar;
	}

}
*/




class OneWaySort extends SortOrder
{
	
	
	function getImage($order) {
		$img = '';
		if($order) {
			$img = ($order == 1) ? $this->img_asc : $this->img_desc;
			$img = '<img src="'.$this->img_path . $img.'">';
		} else {
			$img = '<div style="padding: 4px"></div>';
			//$img = '<img src="'.$this->spacer_img_path.'" height="9" width="9">';
		}
		
		return $img;
	}
	
	
	// get order return num
	function getNumOrder($order) {
		if($order === false) { $order = $this->default_order; }
		else { 
			if(is_numeric($order)) {
				$order = ($order == 2) ? 1 : 2;
			} else {
				$order = ($order == 'desc') ? 1 : 2;
			}
		}
		
		return $order;
	}
	
	
	function & getVars() {
	
		foreach($this->sort_ar AS $tpl_var => $v) {
		
			$order = $this->getOrder($v['order'], $v['get_param']);
			//echo "<pre>"; print_r($order); echo "</pre>";
			
			$num_order = $this->getNumOrder($order);
			//echo "<pre>"; print_r($num_order); echo "</pre>";
			
			// short message
			$title = $this->title_msg[$num_order];
			if(is_array($v['field_name'])) {
				$title = $v['field_name'][0] . ' - ' . $title;
				$v['field_name'] = substr($v['field_name'][0], 0, $v['field_name'][1]);
			}
			
			$ar[$tpl_var]['link']  = $this->getLink($v['get_param'], $num_order);
			$ar[$tpl_var]['img']   = $this->getImage($order);
			$ar[$tpl_var]['field'] = $v['field_name'];
			$ar[$tpl_var]['title'] = $title;
		}
		
		return $ar;
	}
	
	
/*
	function toHtml() {
	
		$a = array();
		foreach($this->getVars() as $k => $v) {
			$a[0] = '<div align="right" style="float: right;">&nbsp;&nbsp;'.$v['img'].'</div>';
			$a[2] = '<a href="'.$v['link'].'" title="'.$v['title'].'" class="'.$this->a_class.'">'.$v['field'].'</a>';
			
			$arr[$k] = implode('', $a);
		}
		
		
		return $arr;
	}
*/


	function toHtml() {

		$a = array();
		foreach($this->getVars() as $k => $v) {
			$a[0] = '<table cellpadding="0" cellspacing="0" width="100%"><tr>';
			$a[1] = '<td><a href="'.$v['link'].'" title="'.$v['title'].'" class="'.$this->a_class.'">'.$v['field'].'</a></td>';			
			$a[2] = '<td align="right" style="padding-left: 5px;">'.$v['img'].'</td>';
			$a[3] = '</tr></table>';			
			
			$arr[$k] = implode('', $a);
		}
		
		
		return $arr;
	}
}
?>